package com.gxuwz.weather.citymanager;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.gxuwz.weather.MainActivity;
import com.gxuwz.weather.R;
import com.gxuwz.weather.adapter.CityManagerAdapter;
import com.gxuwz.weather.db.DBHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CityManagerActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView back, delete, add;
    private ListView cityLv;
    private DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_manager);

        back = findViewById(R.id.city_iv_back);
        delete = findViewById(R.id.city_iv_delete);
        add = findViewById(R.id.city_iv_add);
        cityLv = findViewById(R.id.city_lv);

        dbHelper = new DBHelper(this);

        // 加载 myarea 表中的数据
        loadMyAreaData();

        // 添加点击事件
        back.setOnClickListener(this);
        delete.setOnClickListener(this);
        add.setOnClickListener(this);
    }

    @SuppressLint("Range")
    private void loadMyAreaData() {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query("myarea", new String[]{"areaCn", "cityCn", "areaId", "areaCode"},
                null, null, null, null, null);

        List<Map<String, String>> resultList = new ArrayList<>();
        while (cursor.moveToNext()) {
            Map<String, String> cityInfo = new HashMap<>();
            cityInfo.put("areaCn", cursor.getString(cursor.getColumnIndex("areaCn")));
            cityInfo.put("cityCn", cursor.getString(cursor.getColumnIndex("cityCn")));
            cityInfo.put("areaId", cursor.getString(cursor.getColumnIndex("areaId")));
            cityInfo.put("areaCode", cursor.getString(cursor.getColumnIndex("areaCode")));
            resultList.add(cityInfo);
        }
        cursor.close();

        CityManagerAdapter adapter = new CityManagerAdapter(this, resultList);
        cityLv.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.city_iv_back:
                intent.setClass(this, MainActivity.class);
                startActivity(intent);
                break;
            case R.id.city_iv_delete:
                intent.setClass(this, CityDeleteActivity.class);
                startActivity(intent);
                break;
            case R.id.city_iv_add:
                intent.setClass(this, CityAddActivity.class);
                startActivity(intent);
                break;
        }
    }
}
